﻿using Abp.Configuration;
using Abp.Configuration.Startup;
using Abp.MultiTenancy;
using Abp.Runtime.Session;
using Mt.Site.Core.Base.Settings;
using Mt.Site.Core.Navigations.Navigation;
using Mt.Site.Core.Organizations;
using Mt.Site.Core.Organizations.Posts;
using Mt.Site.Core.Organizations.UserCache;
using System.Collections.Generic;
using System.Linq;

namespace Mt.Site.Core.Configuration
{
    public class CustomConfigProvider : ICustomConfigProvider
    {
        public Dictionary<string, object> GetConfig(CustomConfigProviderContext customConfigProviderContext)
        {
            var config = new Dictionary<string, object>
            {
                {
                    "postLevel", EnumHelper.GetEnumList<PostGroup>().OrderBy(c => c.Value)

                },
                {
                    "postLevelValToName", EnumHelper.GetEnumDictionary<PostGroup>()
                } ,
                {
                    "postLevelNameToVal", EnumHelper.GetEnumDictionary2<PostGroup>()
                } ,
                {
                    "newPostLevelValToName", EnumHelper.GetEnumDictionary<NewPostLevel>()
                } ,
                {
                    "newPostLevelNameToVal", EnumHelper.GetEnumDictionary2<NewPostLevel>()
                }
            };

            var AbpSession = customConfigProviderContext.IocResolver.Resolve<IAbpSession>();
            var _userAndUnitFinder = customConfigProviderContext.IocResolver.Resolve<IUserAndUnitFinder>();
            var _tenantStore = customConfigProviderContext.IocResolver.Resolve<ITenantStore>();
            var _userNavigationManager = customConfigProviderContext.IocResolver.Resolve<IMtUserNavigationManager>();
            var _webConfigSettings = customConfigProviderContext.IocResolver.Resolve<IWebConfigSettings>();

            var userInfo = _userAndUnitFinder.GetCurrentUserInfo();
            if (userInfo != null)
            {
                config.Add("oaUser", userInfo);
            }

            var currentUser = GetCurrentUserInfo(AbpSession, _userAndUnitFinder, _tenantStore);
            if (currentUser != null)
            {
                config.Add("currentUser", currentUser);
            }

            config.Add("backgroundJobs", MtSiteConsts2.BackgroundJobs);
            config.Add("rsaPublicKey", _webConfigSettings.GetValue("RSA-PublicKey"));
            config.Add("aliyunOss", _webConfigSettings.GetFromSettings("Aliyun:IsEnabled") == "true");

            var _mtConfiguration = customConfigProviderContext.IocResolver.Resolve<IMtConfiguration>();
            config.Add("fileUploadAllowed", _mtConfiguration.FileUploadAllowed_TenantSide);
            config.Add("fileUploadMaxSize", _mtConfiguration.FileUploadMaxSize);

            return config;
        }

        private CurrentUserInfo GetCurrentUserInfo(IAbpSession abpSession, IUserAndUnitFinder userAndUnitFinder, ITenantStore tenantStore)
        {
            if (abpSession.ImpersonatorUserId.HasValue)
            {
                var info = userAndUnitFinder.GetUserInfo(abpSession.ImpersonatorTenantId, abpSession.ImpersonatorUserId.Value);
                if (info == null)
                {
                    return null;
                }

                var tenantName = abpSession.ImpersonatorTenantId.HasValue ?
                    tenantStore.Find(abpSession.ImpersonatorTenantId.Value).TenancyName : null;

                return new CurrentUserInfo
                {
                    IsImpersonator = true,
                    TenantId = abpSession.ImpersonatorTenantId,
                    UserId = abpSession.ImpersonatorUserId.Value,
                    TenantName = tenantName,
                    UserName = info.UserName,
                    SurName = info.Surname,
                    LoginName = info.LoginName
                };
            }
            else if (abpSession.UserId.HasValue)
            {
                var info = userAndUnitFinder.GetUserInfo(abpSession.TenantId, abpSession.UserId.Value);
                if (info == null)
                {
                    return null;
                }

                var tenantName = abpSession.TenantId.HasValue ?
                    tenantStore.Find(abpSession.TenantId.Value).TenancyName : null;

                return new CurrentUserInfo
                {
                    IsImpersonator = false,
                    TenantId = abpSession.TenantId,
                    UserId = abpSession.UserId.Value,
                    TenantName = tenantName,
                    UserName = info.UserName,
                    SurName = info.Surname,
                    LoginName = info.LoginName
                };
            }
            else
            {
                return null;
            }
        }
    
    
    }
}
